package day2_linkedlist.leetcode;

/**
 * 删除排序链表中的重复元素,保留一次重复结点
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
 */
public class Num83 {
    //    public ListNode deleteDuplicates(ListNode head) {
//        if (head == null || head.next == null) {
//            return head;
//        }
//        ListNode cur = head;
//        while (cur.next != null) {
//            if (cur.val == cur.next.val) {
//                cur.next = cur.next.next;
//            }else {
//                cur = cur.next;
//            }
//        }
//        return head;
//    }

    /**
     * 递归法
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 第二个节点之后的重复元素交给子函数处理
        head.next = deleteDuplicates(head.next);
        if (head.val != head.next.val) {
            return head;
        }
        return head.next;
    }
}
